package com.mk.cloud.pt.service;

import act.apidoc.Description;
import act.controller.Controller;
import com.mk.cloud.Application;
import com.mk.cloud.common.domain.GlArea;
import com.mk.cloud.common.domain.GlBuyers;
import com.mk.cloud.common.interceptor.ITransacte;
import com.mk.cloud.common.interceptor.cookie.CookieService;
import com.mk.cloud.common.interceptor.cookie.ICLogin;
import org.osgl.util.S;import com.mk.cloud.common.util.S2;
import org.apache.commons.lang3.StringUtils;
import org.beetl.sql.core.engine.PageQuery;
import org.osgl.mvc.annotation.GetAction;
import org.osgl.mvc.annotation.PostAction;
import org.osgl.mvc.annotation.PutAction;
import org.osgl.mvc.annotation.With;

import javax.inject.Inject;
import javax.validation.constraints.NotNull;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller("/frontend/glBuyers")
public class GlBuyersService  extends CookieService {

    @Inject
    private GlBuyers.Mapper glBuyersMapper;

    @Description("*客户端-客户-客户管理-获取列表数据")
    @With(ICLogin.class)
    @GetAction("list.json")
    public PageQuery<GlBuyers> showList(@Description("系统为买家生成的名称店铺加联系人，如（淘宝-张三）") String gbalias, @Description("买家联系人") String gbcontacts, @Description("联系电话") String gbphone, @NotNull @Description("当前第几页") int currentPage, @NotNull @Description("每页多少条") int pageSize) {
        PageQuery<GlBuyers> query = new PageQuery<>();
        query.setPageSize(pageSize);
        query.setPageNumber(currentPage);
        Map<String, Object> params = new HashMap<>();
        params.put("gbcontacts", gbcontacts);
        params.put("gbphone", gbphone);
        params.put("gbalias", gbalias);

        query.setParas(params);

        return glBuyersMapper.getSQLManager().pageQuery("glBuyers.select_list", GlBuyers.class, query);
    }

    @Description("*客户端-客户-客户管理-获取列表数据")
    @With(ICLogin.class)
    @GetAction("selectQuery.json")
    public List<GlBuyers> selectQuery() {
        Map<String, Object> params = new HashMap<>();

        return glBuyersMapper.getSQLManager().select("glBuyers.select_list", GlBuyers.class, params);
    }

    @Description("*客户-客户管理-新增")
    @PostAction("add.json")
    @With({ICLogin.class, ITransacte.class})
    public GlBuyers add(@Description("新增买家") @NotNull GlBuyers glBuyers) {
        String id = S2.UUID();
        glBuyers.setGbid(id);
        glBuyersMapper.insertTemplate(glBuyers);

        return glBuyers;
    }

    @Description("*客户-客户管理-更新")
    @PutAction("update.json")
    @With({ICLogin.class, ITransacte.class})
    public GlBuyers update(@Description("更新买家") @NotNull GlBuyers glBuyers) {
        glBuyersMapper.updateTemplateById(glBuyers);

        return glBuyers;
    }

    @Description("*客户-保存客户，从中获取order")
    @With({ICLogin.class, ITransacte.class})
    public void saveBuyerByOsOrder(@Description("购买人对象") GlBuyers glBuyers){
        GlBuyers buyers = new GlBuyers();
        buyers.setGsid(glBuyers.getGsid());
        buyers.setGbphone(glBuyers.getGbphone());
        List<GlArea> areas = Application.AREAS;
        List<GlBuyers> buyersList = glBuyersMapper.template(buyers);
        String provinceName = StringUtils.replace(glBuyers.getGbprovincename(), "省", "") ;
        String cityName = StringUtils.replace(glBuyers.getGbcityname(), "市", "") ;
        String areaName = StringUtils.replace(glBuyers.getGbareaname(), "区", "") ;
        if(buyersList!=null&&buyersList.size()>0){
            buyersList.forEach(buyer->{
                glBuyers.setGbid(buyer.getGbid());
                for(GlArea area:areas) {
                    if(StringUtils.contains(area.getAreaname(),provinceName)) {
                        glBuyers.setGbprovincecode(area.getAreacode());
                        break;
                    }
                }
                for(GlArea area:areas) {
                    if(StringUtils.contains(area.getAreaname(),cityName)) {
                        glBuyers.setGbcitycode(area.getAreacode());
                        break;
                    }
                }
                for(GlArea area:areas) {
                    if(StringUtils.contains(area.getAreaname(),areaName)) {
                        glBuyers.setGbareacode(area.getAreacode());
                        break;
                    }
                }

                this.update(glBuyers);
            });
        }else {
            for(GlArea area:areas) {
                if(StringUtils.contains(area.getAreaname(),provinceName)) {
                    glBuyers.setGbprovincecode(area.getAreacode());
                    break;
                }
            }
            for(GlArea area:areas) {
                if(StringUtils.contains(area.getAreaname(),cityName)) {
                    glBuyers.setGbcitycode(area.getAreacode());
                    break;
                }
            }
            for(GlArea area:areas) {
                if(StringUtils.contains(area.getAreaname(),areaName)) {
                    glBuyers.setGbareacode(area.getAreacode());
                    break;
                }
            }
            this.add(glBuyers);
        }
    }
}
